/** \file    Positions.cpp
  * \brief   Determines boundingBox and center for each segment.
  * \date    2001
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/segm/src/Positions.cpp,v 1.2 2005/04/04 04:10:16 cscom1r4 Exp $
  */

#include "segm/Segmentation.h"
#include "xtd/arr2d_wise_iterator.h"

void Segmentation::positions() const
{
	if( m_positionsFound ) return;

	int i;
	for( i = 0; i != count(); ++i ) {
		segmentByIndex(i).centre = double_point(0,0);
	}
	BoolArr notMeet( count(), true );
	xtd::arr2d_wise_const_iterator<int> sgIt( m_segmentCoordMap, 0, 0 );
	for( ; sgIt != m_segmentCoordMap.end(); ++sgIt ) {
		int sg = *sgIt;
		if( sg == -1 ) continue;
		//centres
		segmentByIndex(sg).centre.x += sgIt.x();
		segmentByIndex(sg).centre.y += sgIt.y();
		//boundingBoxes
		if( notMeet[sg] ) {
			notMeet[sg] = false;
			segmentByIndex(sg).boundingBox = int_rect(sgIt.x(), sgIt.y(), sgIt.x()+1, sgIt.y()+1);
		} else {
			segmentByIndex(sg).boundingBox.include( point<int>( sgIt.x(), sgIt.y() ) );
		}
	}
	for( i = 0; i != count(); ++i ) {
		segmentByIndex(i).centre /= segmentByIndex(i).pixelCount;
	}

	m_positionsFound = true;
}
